package zhengqc.queue;

import zhengqc.heap.Heap;
import zhengqc.heap.HeapType;
import zhengqc.heap.MaxHeap;
import zhengqc.heap.MinHeap;

public class PriorityQueue<E extends Comparable<E>> implements Queue<E> {
    Heap<E> heap;
    public HeapType heap_type;
    public PriorityQueue(int capacity, HeapType heap_type) {
        this.heap_type = heap_type;
        switch (heap_type) {
            case MaxHeap:
                this.heap = new MaxHeap<>(capacity);
                break;
            default:
                this.heap = new MinHeap<>(capacity);
                break;
        }
    }
    public PriorityQueue(int capacity) {
        this(capacity, HeapType.MinHeap);
    }
    public PriorityQueue(HeapType heap_type) {
        this.heap_type = heap_type;
        switch (heap_type) {
            case MaxHeap:
                this.heap = new MaxHeap<>();
                break;
            default:
                this.heap = new MinHeap<>();
                break;
        }
    }
    public PriorityQueue() {
        this(HeapType.MinHeap);
    }
    public PriorityQueue(E[] array, HeapType heap_type) {
        switch (heap_type) {
            case MaxHeap:
                this.heap = new MaxHeap<>(array);
                break;
            default:
                this.heap = new MinHeap<>(array);
                break;
        }
    }
    public PriorityQueue(E[] array) {
        this(array, HeapType.MinHeap);
    }
    @Override
    public int getSize() {
        return this.heap.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.heap.isEmpty();
    }

    @Override
    public void enqueue(E element) {
        this.heap.append(element);
    }

    @Override
    public E dequeue() {
        return this.heap.extract();
    }

    @Override
    public E getFront() {
        return this.heap.peek();
    }

    @Override
    public void clear() {
        this.heap.clear();
    }

    @Override
    public String toString() {
        return String.format("PriorityQueue %s", this.heap.toString());
    }

    public Object[] toArray() {
        Object[] array = this.heap.toArray();
        return array;
    }
}
